package com.rbac.modules.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.rbac.modules.system.entity.SysDept;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface SysDeptMapper extends BaseMapper<SysDept> {
    
    /**
     * 查询子部门ID列表
     * @param parentId 父部门ID
     * @return 子部门ID列表
     */
    @Select("SELECT id FROM sys_dept WHERE parent_id = #{parentId} AND is_deleted = 0")
    List<Long> selectChildrenIds(@Param("parentId") Long parentId);
    
    /**
     * 查询所有子部门ID列表（包括子部门的子部门）
     * @param parentId 父部门ID
     * @return 所有子部门ID列表
     */
    @Select("WITH RECURSIVE dept_tree AS (" +
            "  SELECT id FROM sys_dept WHERE parent_id = #{parentId} AND is_deleted = 0" +
            "  UNION ALL" +
            "  SELECT d.id FROM sys_dept d" +
            "  INNER JOIN dept_tree dt ON d.parent_id = dt.id" +
            "  WHERE d.is_deleted = 0" +
            ") SELECT id FROM dept_tree")
    List<Long> selectAllChildrenIds(@Param("parentId") Long parentId);
} 